package FebruaryOn2022;

import com.sun.javafx.collections.MappingChange;

import java.util.*;

/**
 * Copyright (c) XuQunXing Technologies Co., Ltd. 2021-2021. All rights reserved.
 * Description:
 * User: x30003433
 * Date: 2022-02-08
 * Time: 9:41
 */
public class GridIllumination {
    public static void main(String[] args) {
        int n = 5;
        int[][] lamps = {{0, 0}, {4, 4}};
        int[][] querie = {{1, 1}, {1, 1}};
        System.out.println(Arrays.toString(gridIllumination(n, lamps, querie)));
    }

    public static int[] gridIllumination(int n, int[][] lamps, int[][] queries) {
        // 1.去重并采集灯的位置信息
        Map<Integer, Integer> row = new HashMap<>();
        Map<Integer, Integer> col = new HashMap<>();
        Map<Integer, Integer> positive = new HashMap<>();
        Map<Integer, Integer> opposite = new HashMap<>();
        Set<Long> set = new HashSet<>();

        for (int[] lamp : lamps
        ) {
            int r = lamp[0];
            int c = lamp[1];
            if (!set.add(hash(r, c))) {
                continue;
            }
            row.put(r, row.getOrDefault(r, 0) + 1);
            col.put(c, col.getOrDefault(c, 0) + 1);
            positive.put(r - c, positive.getOrDefault(r - c, 0) + 1);
            opposite.put(r + c, opposite.getOrDefault(r + c, 0) + 1);
        }
        // 2.遍历查询数组判断对应位置四条线是否有灯
        int[] ans = new int[queries.length];
        for (int i = 0; i < queries.length; i++) {
            int r = queries[i][0];
            int c = queries[i][1];
            if (row.getOrDefault(r, 0) > 0) {
                ans[i] = 1;
            }
            if (col.getOrDefault(c, 0) > 0) {
                ans[i] = 1;
            }
            if (positive.getOrDefault(r - c, 0) > 0) {
                ans[i] = 1;
            }
            if (opposite.getOrDefault(r + c, 0) > 0) {
                ans[i] = 1;
            }

            // 3.去除对应点周围的灯
            for (int x = r - 1; x <= r + 1; x++) {
                for (int y = c - 1; y <= c + 1; y++) {
                    // 去除边界以外的点
                    if (x < 0 || y < 0 || x >= n || y >= n) {
                        continue;
                    }
                    if (set.remove(hash(x, y))) {
//                        System.out.println("haha");
                        row.put(x, row.get(x) - 1);
                        if (row.get(x) == 0) {
                            row.remove(x);
                        }
                        col.put(y, col.get(y) - 1);
                        if (col.get(y) == 0) {
                            col.remove(y);
                        }
                        positive.put(x - y, positive.get(x - y) - 1);
                        if (positive.get(x - y) == 0) {
                            positive.remove(x - y);
                        }
                        opposite.put(x + y, opposite.get(x + y) - 1);
                        if (opposite.get(x + y) == 0) {
                            opposite.remove(x + y);
                        }
                    }
                }
            }
        }
        return ans;
    }

    public static Long hash(int x, int y) {
        return (long) x + ((long) y << 32);
    }
}
